// pages/marketing/index.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "全民营销", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    showMonment: false,
    showReason: false,

    authData: {
      authType: '0', //0表示使用登录弹窗  1表示使用完善昵称头像弹窗 2表示直接弹出头像昵称选择框
      authUsername: "false", //是否已经授权头像昵称
    },
    showLogin: false, //是否显示授权弹窗
    showWsInfoDom: false, //直接显示头像昵称弹窗
    showPrivacyFlag: app.globalData.showPrivacyFlag,

    openid: '',
    is_nickAndHead: 0,
    nickname: '',
    head: '',
    shareTitle: '',
    shareImg: '',
    info: [],
    list: [],
    is_set: 0,
    bh_content: '',
    is_show_qm:0,
    is_del_show:0,
    nameStr:''//被删除好友的姓名
  },
  //点击规则说明
  rule() {
    let that = this;
    that.setData({
      showMonment: true
    })
  },
  lookDetail(){
    wx.navigateTo({
      url: '/pages/marketing/roleSet?look=2'
    })
  },
  //点击我知晓了
  closeMonment() {
    let that = this;
    that.setData({
      showMonment: false
    })
  },

  //查看驳回原因
  showReason(e) {
    let that = this;
    var index = e.currentTarget.dataset.index;
    var bh_content = that.data.list[index].bh_content;
    that.setData({
      showReason: true,
      bh_content: bh_content
    })
  },

  //关闭驳回原因
  closeReason() {
    let that = this;
    that.setData({
      showReason: false,
      bh_content: ''
    })
  },

  //点击身份设置
  set() {
    wx.navigateTo({
      url: '/pages/marketing/roleSet'
    })
  },

  //添加好友
  addFriend() {
    //判断是否设置过身份了
    if (this.data.is_set == 0) {
      app.showTip('请先设置身份后再添加好友哦');
      return;
    }
    wx.navigateTo({
      url: '/pages/marketing/addFriend',
    })
  },

  //查看好友详情
  goDetail(e) {
    var that=this;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/marketing/detail?id='+id
    });
  },

  //删除好友
  del(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;

    wx.showModal({
      title: '提示',
      content: '是否确定删除该好友',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '好友删除中...'
          })
          wx.request({
            url: that.data.baseUrl + 'delMarketingFriend.php',
            method: 'POST',
            data: {
              'openid': that.data.openid,
              'id': id
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              wx.hideLoading();
              app.showTip(res.data.msg);
              if (res.data.status == 1) {
                //后台请求成功
                //删除此下标
                var list = that.data.list;
                list.splice(index,1);
                //重置数组
                that.setData({
                  list:list
                });
              }
            },
            fail: function () {
              wx.hideLoading();
              app.showTip('抱歉,服务器繁忙');
              return;
            }
          });
        } else if (res.cancel) {

        }
      }
    })
  },

  //关闭删除提示弹窗
  delClose(){
    this.setData({
        is_del_show:0
    });
  },

  //关闭更新动态弹窗
  tipsClose(){
    this.setData({
        is_show_qm:0
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;

    var openid = wx.getStorageSync('ly24_openid');
    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getMarketingIndexPageInfo(res.data.openid);
              },
              fail: function (res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getMarketingIndexPageInfo(openid);
    }

    //调用分享
    that.getShareInfo();
  },

  //获取本页数据
  getMarketingIndexPageInfo(openid) {
    var that = this;
    wx.showLoading({
      title: '数据加载中...'
    })
    wx.request({
      url: that.data.baseUrl + 'getMarketingIndexPageInfo.php',
      method: 'POST',
      data: {
        'openid': openid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //后台请求成功
          that.setData({
            is_set: res.data.is_set,
            head: res.data.head,
            is_nickAndHead: res.data.is_nickAndHead,
            info: res.data.info,
            list: res.data.list,
            is_show_qm: res.data.is_show_qm,
            is_del_show:res.data.is_del_show,
            nameStr:res.data.nameStr
          });

          //判断是否默认第一次弹规则
          if(wx.getStorageSync('show_qm_rule')!=1){
              that.setData({
                showMonment:true
              });
              //保存弹规则记录至缓存
              wx.setStorageSync('show_qm_rule',1);
          }
        } else {
          app.showTip(res.data.msg);
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        }
      },
      fail: function () {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    });
  },

  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function (res) {}
    });
  },

  //处理完善信息--不包含隐私协议
  addWxInfo2() {
    this.setData({
      showWsInfoDom: true
    });
  },

  //接收子组件电话授权及头像昵称授权后后返回的值
  returnVal(e) {
    var that = this;
    app.showTip('个人信息已完善');

    //授权完头像
    this.setData({
      is_nickAndHead: e.detail.is_nickAndHead,
      head: that.data.baseUrl + e.detail.head,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/index/index',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  }
})